
/**
 * @author 吴汶
 * @since 2022/10/11
 * 22:42
 */
package com.windgo.service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.windgo.bean.MarketRegion;
import com.windgo.bean.MarketRegionExample;
import com.windgo.bean.common.CommonData;
import com.windgo.bean.regionvo.*;
import com.windgo.bean.common.MarketRegionInfo;
import com.windgo.mapper.MarketRegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RegionServiceImpl implements RegionService {
    @Autowired
    MarketRegionMapper marketRegionMapper;

    @Override
    public CommonData select(MarketRegion marketRegion) {
        Byte type=1;
        Integer pid=0;
        MarketRegionExample marketRegionExample = new MarketRegionExample();
        MarketRegionExample.Criteria criteria = marketRegionExample.createCriteria();
        criteria.andPidEqualTo(pid);
        criteria.andTypeEqualTo(type);
        long l = marketRegionMapper.countByExample(marketRegionExample);
        List<RegionVo4> marketRegions = marketRegionMapper.selectByAllChildren(pid);
        for (RegionVo4 region : marketRegions) {
            Integer id = region.getId();
           /* MarketRegionExample marketRegionExample1 = new MarketRegionExample();
            MarketRegionExample.Criteria criteria1 = marketRegionExample1.createCriteria();
            criteria1.andPidEqualTo(id);

            */
            List<RegionVo2> marketRegions1 = marketRegionMapper.selectChildrenById(id);
            region.setChildren(marketRegions1);
            for (RegionVo2 regionVo2 : marketRegions1) {
                Integer id1 = regionVo2.getId();
               /* MarketRegionExample marketRegionExample2 = new MarketRegionExample();
                MarketRegionExample.Criteria criteria2 = marketRegionExample2.createCriteria();
                criteria2.andPidEqualTo(id1);

                */
                List<RegionVo3> marketRegions2 = marketRegionMapper.selectAllById(id1);
                regionVo2.setChildren(marketRegions2);
            }

        }
        CommonData<RegionVo4> regionVoCommonData = new CommonData<>();
        regionVoCommonData.setTotal((int)l);
        regionVoCommonData.setPages(1);
        regionVoCommonData.setPage(1);
        regionVoCommonData.setLimit(31);
        regionVoCommonData.setList(marketRegions);
        return regionVoCommonData;
    }
    public List<GranChildrenVo> selectPro(MarketRegion marketRegion) {
       MarketRegionExample marketRegionExample = new MarketRegionExample();
        MarketRegionExample.Criteria criteria = marketRegionExample.createCriteria();
        criteria.andPidEqualTo(marketRegion.getId());
        List<GranChildrenVo> marketRegions = marketRegionMapper.selectById(marketRegion.getId());

        return marketRegions;

    }


    /*@Override
    public RegionVo select(RegionVo regionVo, Integer pid) {
        PageHelper.startPage(regionVo.getPage(), regionVo.getLimit());
        MarketRegionExample marketRegionExample = new MarketRegionExample();
        MarketRegionExample.Criteria criteria = marketRegionExample.createCriteria();

        if(pid==0){
            criteria.andPidEqualTo(pid);
        }
        List<MarketRegion> marketRegions = marketRegionMapper.selectByExample(marketRegionExample);
        for (MarketRegion marketRegion : marketRegions) {

        }
        /*ProvinceRegionVo provinceRegionVo = new ProvinceRegionVo();

        for (MarketRegion marketRegion : marketRegions) {
            MarketRegionExample marketRegionExample1 = new MarketRegionExample();
            MarketRegionExample.Criteria criteria1 = marketRegionExample1.createCriteria();
            Integer id = marketRegion.getId();
            if(id!=null){
                criteria1.andPidEqualTo(id);
                List<MarketRegion> children = marketRegionMapper.selectByExample(marketRegionExample1);
                ChildrenRegionVo childrenRegionVo = new ChildrenRegionVo();
                ArrayList<ChildrenRegionVo> list = new ArrayList<>();
                for (MarketRegion child : children) {
                    MarketRegionExample marketRegionExample2 = new MarketRegionExample();
                    MarketRegionExample.Criteria criteria2 = marketRegionExample2.createCriteria();
                    Integer id1 = child.getId();
                    if(id1!=null){
                        criteria2.andPidEqualTo(id1);
                    }
                    List<MarketRegion> children1 = marketRegionMapper.selectByExample(marketRegionExample2);
                    childrenRegionVo.setChildren(children1);
                    Integer id2 = child.getId();
                    String name = child.getName();
                    Byte type = child.getType();
                    Integer code = child.getCode();
                     childrenRegionVo.setId(id2);
                     childrenRegionVo.setName(name);
                     childrenRegionVo.setType(type);
                     childrenRegionVo.setCode(code);

                }
               // list.add(childrenRegionVo);

            }


        }

         */
    //}
   /* @Autowired
    MarketRegionMapper regionMapper;

    @Override
    public RegionDataVo selectRegionData(BaseRegionPageInfoVo infoVo) {
        //BasePageInfoVo basePageInfoVo1 = new BasePageInfoVo();
        RegionDataVo data = new RegionDataVo();

        //在最上面开启分页
      //  PageHelper.startPage(infoVo.getPage(), infoVo.getLimit());
        //首先计算出有多少个省，包括自治区以及直辖市
        MarketRegionExample marketRegionExample = new MarketRegionExample();
        MarketRegionExample.Criteria criteria = marketRegionExample.createCriteria();
        MarketRegionExample.Criteria criteria1 = criteria.andPidEqualTo(0);
        //这里获取的是最外层的数据
        ArrayList<ChildrenVo> children = new ArrayList<>();
        ArrayList<RegionVo> list = new ArrayList<>();

        List<MarketRegionInfo> marketRegionInfos = regionMapper.selectRegionInfo(marketRegionExample);
        for (MarketRegionInfo marketRegionInfo : marketRegionInfos) {
            RegionVo regionVo = new RegionVo();
            Integer id = marketRegionInfo.getId();
            Integer code = marketRegionInfo.getCode();
            String name = marketRegionInfo.getName();
            Integer type = marketRegionInfo.getType();
            regionVo.setId(id);
            regionVo.setName(name);
            regionVo.setType(type);
            regionVo.setCode(code);
            List<MarketRegionInfo> marketRegionInfos1 = regionMapper.selectChildrenRegionInfo(id);
            for (MarketRegionInfo regionInfo : marketRegionInfos1) {
                ChildrenVo childrenVo = new ChildrenVo();
                childrenVo.setId(regionInfo.getId());
                childrenVo.setName(regionInfo.getName());
                childrenVo.setType(regionInfo.getType());
                childrenVo.setCode(regionInfo.getCode());
                Integer id1 = regionInfo.getId();
                List<GranChildrenVo> granChildrenVos = regionMapper.selectGranChildrenRegionInfo(id1);
                childrenVo.setChildren(granChildrenVos);
                children.add(childrenVo);
            }
            regionVo.setChildren(children);
            list.add(regionVo);
        }
        PageInfo<MarketRegionInfo> objectPageInfo = new PageInfo<MarketRegionInfo>();
        long total = objectPageInfo.getTotal();
        int pages = objectPageInfo.getPages();
        int pageSize = objectPageInfo.getPageSize();
        int pageNum = objectPageInfo.getPageNum();
        data.setLimit(pageSize);
        data.setList(list);
        data.setPage(pageNum);
        data.setPages(pages);
        data.setTotal(total);
        return data;
    }
    
    */

}

